package pl.register.android.activities.tasks.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import pl.register.android.R;
import pl.register.android.activities.tasks.TasksListFragment;
import pl.register.android.task.Task;
import pl.register.android.util.ListItemMode;
import pl.register.android.widget.listview.RefreshableAdapter;

import java.util.Collections;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-09
 */

public class TasksListAdapter extends RefreshableAdapter<Task> implements View.OnClickListener {

    private ListItemMode itemsMode;

    private TasksListFragment fragment;

    public TasksListAdapter(Context context, TasksListFragment fragment) {
        super(context);
        this.fragment = fragment;
        this.itemsMode = ListItemMode.NONE;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_task_row, null);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.task_title_text_view);
            holder.tvCreationDate = (TextView) convertView.findViewById(R.id.task_creation_date_text_view);
            holder.ibtnMode = (ImageButton) convertView.findViewById(R.id.mode_image_button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        fillRow(holder, position);
        return convertView;
    }

    private void fillRow(ViewHolder holder, int position) {
        Task task = items.get(position);
        holder.tvTitle.setText(task.getTitle());
        holder.tvCreationDate.setText(task.getCreationDateInString());
        holder.ibtnMode.setImageDrawable(itemsMode.getIcon(context));
        holder.ibtnMode.setFocusable(false);
        holder.ibtnMode.setTag(task);
        holder.ibtnMode.setOnClickListener(this);
        if (itemsMode.equals(ListItemMode.NONE)) {
            holder.ibtnMode.setVisibility(View.GONE);
        } else {
            holder.ibtnMode.setVisibility(View.VISIBLE);
        }
    }

    public void remove(Task task) {
        items.remove(task);
        refresh();
    }

    public void add(Task task) {
        items.add(task);
        refresh();
    }

    public void changeItemsMode(ListItemMode itemsMode) {
        if (this.itemsMode.equals(itemsMode)) {
            this.itemsMode = ListItemMode.NONE;
        } else {
            this.itemsMode = itemsMode;
        }
        notifyDataSetChanged();
    }

    @Override
    public void refresh() {
        Collections.sort(items);
        super.refresh();
    }

    @Override
    public void onClick(View v) {
        Task task = (Task) v.getTag();
        fragment.showDeleteTaskConfirmationDialog(task);
    }

    public static class ViewHolder {
        public TextView tvTitle;
        public TextView tvCreationDate;
        public ImageButton ibtnMode;
    }
}